import config from "@/../public/config/config.json";
import { defineStore } from "pinia";
import { Instance } from "ys-ts/src/ts/instance";
import * as yaml from "yaml";
import { AxiosUtil } from "ys-ts/src/ts/axios";
const key = () => "config.default";

export type store = typeof config;

export async function loadConfigDefasult() {
  let data: null | store = null;

  const axios = AxiosUtil.newAxios({});
  await axios
    .get<string>(`/config/config.yaml`)
    .then((res) => {
      data = yaml.parse(res.data);
    })
    .catch((err) => {
      console.error("[plugin/store] 默认配置数据读取失败", err);
    });

  Instance.Instance().set<null | store>(key(), data);
}

export const useConfigStore = defineStore({
  id: "config",
  state: (): store => {
    return Instance.Instance().get(key()) as store;
  },
});
